package cn.hchaojie.javase.day15;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.Test;

public class DemoStreams {
	public static void main(String[] args) {
		System.out.println("AA");
		
		try {
			int i = System.in.read();
			System.out.println(1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 使用字节流读取文件
	 */
	
	@Test
	public void testFileRead() {
		try {
			FileInputStream in = new FileInputStream("D:\\camp-bee\\代码\\javase\\En.txt");
			
//			int data = in.read();		// 读第一个
//			System.out.println((char) data);
//			
//			data = in.read();			// 读第二个字节
//			System.out.println((char) data);
			
			
			// 一次读所有字节
			byte[] bytes = new byte[1024];
			int count = in.read(bytes);
			System.out.println(count);
			
			System.out.println(new String(bytes, 0, count));
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testFileRead2() {
		FileInputStream in = null;
		try {
			in = new FileInputStream("D:\\camp-bee\\代码\\javase\\En.txt");
			
//			while (true) {
//				int data = in.read();
//				if (data == -1) break;
//				
//				System.out.println(data);
//			}
			
			int data;
			while ((data = in.read()) != -1) {
				System.out.println(data);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					
				}
			}
		}
	}
	
	// 使用try(resource)形式自动关闭流, 带资源的try语句
	@Test
	public void testFileRead3() {
		// try (resource) 括号里面打开的资源会自动关闭 
		try (
			FileInputStream in = new FileInputStream("D:\\camp-bee\\代码\\javase\\En.txt");
		) {
			int data;
			while ((data = in.read()) != -1) {
				System.out.println(data);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	
	
	// 流需要关闭
	// 覆盖的形式写内容
	@Test
	public void testFileWrite() {
		try (
			FileOutputStream out = new FileOutputStream("En.txt");
		) {
			out.write('a');
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// 追加内容的形式写文件
	@Test
	public void testFileWrite2() {
		try (
			FileOutputStream out = new FileOutputStream("En.txt", true);
		) {
			// 写一个字节
			out.write('1');

			// 写多个字节
			String str = "cdef";
			byte[] bytes = str.getBytes();
			out.write(bytes);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// 文件随机位置的访问（读和写）
	@Test
	public void testRaf() {
		try (
				RandomAccessFile raf = new RandomAccessFile("En.txt", "rw"); // rw: 文件打开之后，可读可写
		) {
			long pointer = raf.getFilePointer();
			System.out.println(pointer);
			
			// 读最后一个字节
			raf.seek(6);		// 文件指针移到要读的位置
			
			int data = raf.read();
			System.out.println((char) data);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	// 复制文件
	@Test
	public void testCopy() {
		try (
			FileInputStream in = new FileInputStream("C:\\Users\\hchaojie\\Desktop\\InletexEMC.exe");
			FileOutputStream out = new FileOutputStream("C:\\Users\\hchaojie\\Desktop\\InletextEMC2.exe");
		) {
			int data;
			while ((data = in.read()) != -1) {
				out.write(data);
			}
		} catch (Exception e) {
		}
	}
	
	@Test
	public void testSize() throws IOException {
		File file = new File("En.txt");
		System.out.println(file.length());
		System.out.println(Files.size(file.toPath()));		// Paths.get("En.txt")
	}
	
	// 断点续传
	@Test
	public void testCopy2() {
		try (
				FileInputStream in = new FileInputStream("C:\\Users\\hchaojie\\Desktop\\InletexEMC.exe");
				FileOutputStream out = new FileOutputStream("C:\\Users\\hchaojie\\Desktop\\InletextEMC2.exe");	
				) {
			
			long size = Files.size(Paths.get("C:\\Users\\hchaojie\\Desktop\\InletexEMC.exe"));
			long random = (long) (Math.random() * size);
			
			int data;
			long count = 0;
			while ((data = in.read()) != -1) {
				// 在一个随机的位置，中断复制
				count++;
				
				if (count >= random) {
					System.out.println("复制中断，已经复制了：" + count);
					break;
				}

				out.write(data);
			}
		} catch (Exception e) {
		}
	}
	

	// 断点续传、复制剩余的文件内容
	@Test
	public void testRemain() {
		String file2 = "C:\\Users\\hchaojie\\Desktop\\InletextEMC2.exe";
		try (
			RandomAccessFile raf = new RandomAccessFile("C:\\Users\\hchaojie\\Desktop\\InletexEMC.exe", "r");
			FileOutputStream out = new FileOutputStream(file2, true);
		) {
			// 把raf的文件指针，移动到文件复制中断的位置（之前复制的文件的大小）
			
			long copied = Files.size(Paths.get(file2));
			raf.seek(copied);
			
			int data;
			while ((data = raf.read()) != -1) {
				out.write(data);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// 断点续传、复制剩余的文件内容
	@Test
	public void testRemain2() {
		String file2 = "C:\\Users\\hchaojie\\Desktop\\InletextEMC2.exe";
		try (
			RandomAccessFile raf = new RandomAccessFile("C:\\Users\\hchaojie\\Desktop\\InletexEMC.exe", "r");
			FileOutputStream out = new FileOutputStream(file2, true);
		) {
			// 把raf的文件指针，移动到文件复制中断的位置（之前复制的文件的大小）
			
			long copied = Files.size(Paths.get(file2));
			raf.seek(copied);
			
			byte[] data = new byte[1024];
			int count = 0;
			while ((count = raf.read(data)) != -1) {
				out.write(data, 0, count);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
